package com.xbh.captcha.service.impl;

import com.wf.captcha.GifCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import com.xbh.captcha.service.CaptchaSaveService;
import com.xbh.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: captcha-provider
 * @description:
 * @author: 许宝华
 * @create: 2021-09-19 12:26
 */
@Service
public class CaptchaSaveServiceImpl implements CaptchaSaveService {

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 生成gif类型验证码
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @Override
    public String captchaSave(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //生成验证码
        GifCaptcha gifCaptcha = new GifCaptcha(130,48,4);
        String code = gifCaptcha.toBase64();
//        CaptchaUtil.out(gifCaptcha, request, response);
        String verCode = gifCaptcha.text().toLowerCase();
        //将验证码存入redis
        redisUtils.set("code",verCode);
        return code;


    }

    @Override
    public String captchaSavePng(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //生成验证码
        SpecCaptcha pngCaptcha = new SpecCaptcha(130,48,4);
        String code = pngCaptcha.toBase64();
        String verCode = pngCaptcha.text().toLowerCase();
        //将验证码存入redis
        redisUtils.set("code",verCode);
        return code;
    }
}
